package com.sinosoft.dsp.builder.generator;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.velocity.VelocityContext;

import com.sinosoft.builder.config.BusinessConceptHelper;
import com.sinosoft.builder.config.FileHelper;
import com.sinosoft.builder.config.ILoaderHolder;
import com.sinosoft.builder.config.Logger;
import com.sinosoft.builder.generator.GeneratorException;
import com.sinosoft.builder.generator.IGenerator;
import com.sinosoft.builder.generator.InnerGenerator;
import com.sinosoft.builder.utils.BusinessConceptTableType;

/**
 * 用于创建业务概念实体Bean的类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2010-12-9 上午10:41:52
 */
public class BusinessConceptEntityBeanGenerator extends InnerGenerator
		implements IGenerator {

	public static final String BUSINESS_CONCEPT_CONFIG_PACKAGE = "package";

	public static final String BUSINESS_CONCEPT_CONFIG_PREFIX = "prefix";

	private String packageName;

	private String prefix;

	@SuppressWarnings("unchecked")
	public BusinessConceptEntityBeanGenerator(ILoaderHolder config,
			Map businessConceptConfig, Logger log) {
		super(config, log);
		if (businessConceptConfig != null) {
			this.packageName = BusinessConceptHelper
					.getPackageName((String) businessConceptConfig
							.get(BUSINESS_CONCEPT_CONFIG_PACKAGE));
			this.prefix = BusinessConceptHelper
					.getPrefix((String) businessConceptConfig
							.get(BUSINESS_CONCEPT_CONFIG_PREFIX));
		}
	}

	/**
	 * 创建文件
	 */
	@Override
	public void createFiles() throws GeneratorException {
		VelocityContext context = new VelocityContext();

		initContext(context);

		// 获得解析好的业务概念对象
		// for (Iterator<Object> iter = getConfig().getResObjects().iterator();
		// iter.hasNext();) {
		// BusinessConcept bc = (BusinessConcept) iter.next();
		// }

		// 创建信息模型数据库对于的实体文件
		createFileForBusinessConcept(context);

	}

	/**
	 * 创建行为内容关联
	 * 
	 * @param context
	 * @throws GeneratorException
	 */
	private void createFileForBusinessConcept(VelocityContext context)
			throws GeneratorException {
		context.put("package", this.packageName.toLowerCase());
		context.put("prefix", this.prefix.toUpperCase());
		context.put("prefixS", this.prefix.toLowerCase());

		// 通过实体名称和模板
		for (BusinessConceptTableType type : BusinessConceptTableType.values()) {
			String resultPath = (BusinessConceptHelper
					.getBusinessConceptTableClassName((String) context
							.get("package"), (String) context.get("prefix"),
							type)).replace('.', '/')
					+ ".java";
			File javaFile = new File(getDestDir(), resultPath);
			context.put("entityName", type.getEntityName());
			context.put("tableName", type.getTableName());
			type.loadContext(context);
			FileHelper.generatorFile(context, this.getConfig().getTemplate(
					type.getTemplateName()), javaFile);
		}
	}

	private void initContext(VelocityContext context) {
		context.put("generator", this);
		DateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
		context.put("timestamp", dateFormat.format(new Date()));
	}

}
